package com.interview.three;

import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

@Slf4j
public class TestProxy {
    public static void main(String[] args) {
        ProxyFactory factory = new ProxyFactory();
        factory.setTarget(new Target1());
        
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* foo())");
        factory.addAdvisor(new DefaultPointcutAdvisor(pointcut,(MethodInterceptor) invocation -> {
            try {
                log.info("---before---");
                return invocation.proceed(); // 调用目标
            }finally {
                log.error("---after---");
            }

        }));
        Target1 proxy = (Target1) factory.getProxy();
        proxy.bar();
        proxy.foo();
    }
    
    interface InterfaceTem{
        void foo();
        void bar();
    }
    static class Target1 implements InterfaceTem{

        @Override
        public void foo() {
            log.info("Target1 --- foo");
        }

        @Override
        public void bar() {
            log.info("Target1 --- bar");
        }
    }
}
